package commands;

import java.util.Scanner;

import exceptions.ParseException;

import memory.Memory;

public class ReadCommand extends Command {
	
	Command next;
	String variableName;
	Memory memory;
	Scanner in;
	
	public ReadCommand(int lineNumber, Memory memory, Scanner input, String command) throws ParseException {
		super(lineNumber);
		
		command = fixString(command.substring(command.indexOf("read") + 4));
		in = input;
		this.memory = memory;
		if (Memory.isCorrectVariableName(command)) {
			variableName = command;
		} else {
			throw new ParseException(command + " is illegal variable name");
		}
	}

	@Override
	public void run() {
		memory.assign(variableName, in.nextInt());
	}

	@Override
	public Command getNext() {
		return next;
	}

	@Override
	public void setNext(Command command) {
		next = command;
	}

}
